package krt.wid.gslzs.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.yanzhenjie.nohttp.rest.Response;

import krt.wid.gslzs.R;
import krt.wid.gslzs.base.BaseActivity;
import krt.wid.gslzs.bean.LoginBean;
import krt.wid.gslzs.bean.UserInfo;
import krt.wid.gslzs.util.SpUtil;
import krt.wid.gslzs.util.UrlUtil;
import krt.wid.http.MHttp;

import krt.wid.http.MResponseListener;
import krt.wid.http.Result;
import krt.wid.util.MToast;
import krt.wid.util.ParseJsonUtil;

/**
 * @author admin
 * @date 2018/5/8
 * 登录页
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private TextView register;
    private Button subit;
    //账号密码
    MaterialEditText use_name;
    MaterialEditText user_pwd;

    @Override
    public int bindLayout() {
        return R.layout.login;
    }

    @Override
    public void initView() {

        register = findViewById(R.id.zxp_forget_zc);
        use_name = findViewById(R.id.log_user);
        user_pwd = findViewById(R.id.log_ps);
        subit = findViewById(R.id.bt_login);
        findViewById(R.id.zxp_forget_pwd).setOnClickListener(this);
        register.setOnClickListener(this);
        subit.setOnClickListener(this);
        for (String key : mSpUtil.getUseNamePsd().keySet()) {
            if (key.equals("name")) {
                use_name.setText(mSpUtil.getUseNamePsd().get(key));
                use_name.setSelection(mSpUtil.getUseNamePsd().get(key).length());
            } else if (key.equals("psd")) {
                user_pwd.setText(mSpUtil.getUseNamePsd().get(key));
            }
        }

    }

    @Override
    public void loadData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.zxp_forget_zc:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;

            case R.id.bt_login:

                if (use_name.getText().toString().equals("")) {
                    MToast.showToast(LoginActivity.this, "用户名不能为空");
                    break;
                }
                if (user_pwd.getText().toString().equals("")) {
                    MToast.showToast(LoginActivity.this, "密码不能为空");
                    break;
                }
//                 startActivity(new Intent(LoginActivity.this,QyLoginMainActivity.class))
                final String url = "http://111.75.240.74:13381/non_public_economy/api/login";
                final String url2 = "http://111.75.240.74:13381/non_public_economy/api/userInfo";
                MHttp.<Result<LoginBean>>create(this).setUrl(url)
//                       .addParams("username","gx002")
                        .addParams("username", use_name.getText().toString())
                        .addParams("password", user_pwd.getText().toString())
                        .showDialog(false)
                        .execute(new MResponseListener<Result<LoginBean>>() {
                            @Override
                            public void onSuccess(int what, Result<LoginBean> loginBeanResult) {
                                if (loginBeanResult.isSuccess()) {
                                    mSpUtil.setUseNamePsd(use_name.getText().toString(), user_pwd.getText().toString());
                                    mSpUtil.setAccessToken(loginBeanResult.getData().getAccessToken());
                                    MHttp.<Result<UserInfo>>create(LoginActivity.this).setUrl(url2)
                                            .addParams("accessToken", loginBeanResult.getData().getAccessToken())
                                            .execute(new MResponseListener<Result<UserInfo>>() {
                                                @Override
                                                public void onSuccess(int what, Result<UserInfo> userInfo) {
                                                    if (userInfo.isSuccess()) {
                                                        mSpUtil.setUserInfo(ParseJsonUtil.toJson(userInfo.getData()));
                                                        if (userInfo.getData().getRoleCode().equals("ent")) { //判断为企业用户
                                                            startActivity(new Intent(LoginActivity.this, EntMainActivity.class).putExtra("entCode", userInfo.getData().getRoleCode()));
                                                        } else {    //判断为政府用户
                                                            startActivity(new Intent(LoginActivity.this, GovMainActivity.class));
                                                        }
                                                        finish();
                                                    } else {
                                                        MToast.showToast(LoginActivity.this, userInfo.getMsg());
                                                    }

                                                }

                                                @Override
                                                public void onFailed(int what, Response<Result<UserInfo>> response) {

                                                }
                                            });


                                } else {
                                    MToast.showToast(LoginActivity.this, loginBeanResult.getMsg());
                                }
                            }

                            @Override
                            public void onFailed(int what, Response<Result<LoginBean>> response) {

                            }
                        });

                break;
            case R.id.zxp_forget_pwd:
                startActivity(new Intent(LoginActivity.this, WebActivity.class).putExtra("url", UrlUtil.getUrl("WangJiMiMa&goIndex=1")));
                break;
            default:
                break;
        }
    }
}
